package com.ruoyi.web.controller.wxqy;

import com.ruoyi.common.annotation.ControllerAuthority;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.core.cache.Meta;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.NumberUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.model.vo.ContactWayGroupVo;
import com.ruoyi.qywx.model.vo.ContactWayInfo;
import com.ruoyi.qywx.model.vo.ContactWayVo;
import com.ruoyi.qywx.model.vo.WelcomeVo;
import com.ruoyi.qywx.service.inters.ContactWayGroupService;
import com.ruoyi.qywx.service.inters.ContactWayService;
import com.ruoyi.system.service.ISysTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author : hcl
 * @Date : 2023/8/2
 * 联系我controller
 */
@RestController
@RequestMapping("/wxqy/qrcode")
@ControllerAuthority
public class ContactWayController extends BaseController {
    @Autowired
    private ContactWayService contactWayService;
    @Autowired
    private ISysTeamService teamService;
    @Autowired
    private ContactWayGroupService wayGroupService;


    @GetMapping("/list")
    public AjaxResult getContactWay(@RequestParam(required = false, defaultValue = "0") Long groupId,
                                    @RequestParam(required = false) String keyword) {
        startPage();
        List<ContactWayInfo> wayInfos = contactWayService.selectContactWayList(getTeamId(), groupId, keyword);
        return success(getDataTable(wayInfos));
    }

    @PostMapping("/find/{id}")
    public AjaxResult getContactWayById(@PathVariable Long id) {
        if (NumberUtils.isTrue(id)) {
            ContactWayVo vo = contactWayService.selectContactWayById(id, getTeamId());
            if (Objects.nonNull(vo)) {
                return success(vo);
            }
            return error("员工活码不存在");
        }
        return error(ErrorConstants.PARAMS_ERR);
    }

    @PostMapping("/add")
    public AjaxResult qrcodeAdd(@RequestBody ContactWayVo contactWayVo) {
        WelcomeVo welcome = contactWayVo.getWelcome();
        if (welcome == null) {
            return error("未设置欢迎语");
        }
        String err = welcome.validate();
        if (StrUtils.isNotEmpty(err)) {
            return error(err);
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) {
            return error(Constants.QY_WX_TEAM_ERR);
        }
        return contactWayService.addQrcode(contactWayVo, getUserId(), meta);
    }

    @PostMapping("/delete")
    public AjaxResult qrcodeDelete(Long qrcodeId) {
        if (!NumberUtils.isTrue(qrcodeId)) {
            return error(ErrorConstants.PARAMS_ERR);
        }
        Meta meta = teamService.selectTeamMeta(getTeamId());
        if (Objects.isNull(meta)) {
            return error(Constants.QY_WX_TEAM_ERR);
        }
        return contactWayService.delContactWayById(qrcodeId, meta);
    }


    @GetMapping("/group/list")
    public AjaxResult qrcodeGroupList() {
        return AjaxResult.success(wayGroupService.contactWayGroupList(getTeamId()));
    }


    @PostMapping("/group/add")
    public AjaxResult qrcodeGroupAdd(ContactWayGroupVo vo) {
        if (StrUtils.isEmpty(vo.getName())) {
            return error("请输入分组名称");
        }
        return success(wayGroupService.addContactWayGroup(vo, getTeamId()));
    }

    @PostMapping("/group/delete")
    public AjaxResult qrcodeGroupAdd(Long groupId) {
        if (!NumberUtils.isTrue(groupId)) {
            return error("请选择需要删除的分组");
        }
        List<ContactWayInfo> infos = contactWayService.selectContactWayList(getTeamId(), groupId, null);
        if (CollectionUtils.isNotEmpty(infos)) {
            return error("该分组下仍存在员工活码，不能删除~");
        }
        return wayGroupService.delContactWayGroup(groupId, getTeamId());
    }

    @PostMapping("/download/{id}")
    public AjaxResult qrcodeDownload(HttpServletRequest request, @PathVariable Long id) {
        if (!NumberUtils.isTrue(id)) {
            return error(ErrorConstants.PARAMS_ERR);
        }
        return contactWayService.download(request, id, getTeamId());
    }
}
